package view.components;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import controller.Controller;

/**
 * Create and perform menu items
 * 
 * @author Alex Daniel
 */
public class SLogoMenu {
    private JMenuBar myMenuBar;
    private SLogoTabManager myTabManager;
    private Controller myController;

    public SLogoMenu(Controller controller) throws IOException {
        this(controller, new SLogoTabManager("SLogo", controller));
    }

    public SLogoMenu(Controller controller, SLogoTabManager tabManager) {
        myController = controller;
        myMenuBar = this.createMenuBar();
        myTabManager = tabManager;
    }

    /**
     * 
     * @return selected menu bar
     */
    public JMenuBar getMenuBar() {
        return myMenuBar;
    }

    /**
     * pass in the tab manager
     * 
     * @param manager
     */
    public void setTab(SLogoTabManager manager) {
        myTabManager = manager;
    }

    /**
     * create menu bar
     * 
     * @return complete menubar
     */
    private JMenuBar createMenuBar() {
        JMenuBar menuBar = new JMenuBar();

        menuBar.add(this.createFileMenu());
        menuBar.add(this.createEditMenu());

        return menuBar;
    }

    /**
     * creates the 'edit' menu and actions
     * 
     * @return edit JMenu object
     */
    @SuppressWarnings("serial")
    private JMenu createEditMenu() {
        JMenu edit = new JMenu("Edit");

        edit.add(new AbstractAction("Cut") {
            public void actionPerformed(ActionEvent e) {
                myController.setCopy(myTabManager.getInput());
                myController.getView().getCanvas().setInput("");
            }
        });
        edit.add(new AbstractAction("Copy") {
            public void actionPerformed(ActionEvent e) {
                myController.setCopy(myTabManager.getInput());

            }
        });
        edit.add(new AbstractAction("Paste") {
            public void actionPerformed(ActionEvent e) {
                myController.getView().getCanvas()
                        .setInput(myController.getCopy());
            }
        });

        return edit;
    }

    /**
     * Creates the file menu
     * 
     * @return JMenu with action components
     */
    @SuppressWarnings({ "serial", "unused" })
    private JMenu createFileMenu() {
        JMenu menu = new JMenu("File");

        menu.add(new AbstractAction("New") {
            @Override
            public void actionPerformed(ActionEvent e) {
                try {
                    myTabManager.addTab("SLogo", myController);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
            }
        });

        JMenuItem open = new JMenuItem("Open");
        menu.add(new AbstractAction("Open") {
            public void actionPerformed(ActionEvent e) {
                JFileChooser open = new JFileChooser();
                int returnval = open.showOpenDialog(myMenuBar);
                StringBuilder in = new StringBuilder();
                if (returnval == JFileChooser.APPROVE_OPTION) {
                    File file = open.getSelectedFile();
                    try {
                        FileReader fr = new FileReader(file);
                        int data = fr.read();
                        while (data != -1) {
                            in.append((char) data);
                            data = fr.read();
                        }
                    } catch (FileNotFoundException e1) {
                        e1.printStackTrace();
                    } catch (IOException io) {
                        io.printStackTrace();
                    }
                }
                Scanner scan = new Scanner(in.toString());
                List<String> readin = new ArrayList<String>();
                scan.useDelimiter("\n");
                while (scan.hasNext()) {
                    readin.add(scan.next());
                }
                for (String str : readin)
                    myController.execute(str);

            }
        });

        JMenuItem close = new JMenuItem("Close");
        menu.add(new AbstractAction("Close") {
            public void actionPerformed(ActionEvent e) {
                myTabManager.removeTab();
            }
        });

        JMenuItem save = new JMenuItem("Save");
        menu.add(new AbstractAction("Save") {
            public void actionPerformed(ActionEvent e) {
                JFileChooser save = new JFileChooser();
                int returnVal = save.showSaveDialog(myMenuBar);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = save.getSelectedFile();
                    try {
                        FileWriter fw = new FileWriter(file);
                        List<String> temp = myController.getView().getCanvas()
                                .getHistoryCommandsWindow().getHistory();
                        StringBuilder build = new StringBuilder();
                        for (String str : temp) {
                            build.append(str + "\n");
                        }
                        System.out.println(build.toString());
                        fw.write(build.toString());
                        fw.close();
                    } catch (IOException io) {
                        io.printStackTrace();
                    }

                }

            }
        });

        menu.add(new AbstractAction("Quit") {
            public void actionPerformed(ActionEvent e) {
                System.exit(0);
            }
        });

        return menu;
    }
}
